Telegram Group & Telegram Channel
🔧 Задача на C# для внимательных разработчиков

Что выведет следующий код?


using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
var actions = new List<Action>();

for (int i = 0; i < 3; i++)
{
actions.Add(() => Console.WriteLine(i));
}

foreach (var action in actions)
action();
}
}


Варианты ответа:
A)

1
2


B)

3
3


C)

0
0


D)Ошибка компиляции


---

Правильный ответ: B

Почему:
Лямбда-функции захватывают переменную
i по ссылке, а не её значение на каждой итерации. После завершения цикла i == 3, и все замыкания ссылаются на одно и то же i. Это классическая ловушка замыканий в C#.

@csharp_ci



tg-me.com/csharp_ci/1329
Create:
Last Update:

🔧 Задача на C# для внимательных разработчиков

Что выведет следующий код?


using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
var actions = new List<Action>();

for (int i = 0; i < 3; i++)
{
actions.Add(() => Console.WriteLine(i));
}

foreach (var action in actions)
action();
}
}


Варианты ответа:
A)

1
2


B)

3
3


C)

0
0


D)Ошибка компиляции


---

Правильный ответ: B

Почему:
Лямбда-функции захватывают переменную
i по ссылке, а не её значение на каждой итерации. После завершения цикла i == 3, и все замыкания ссылаются на одно и то же i. Это классическая ловушка замыканий в C#.

@csharp_ci

BY C# (C Sharp) programming


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/csharp_ci/1329

View MORE
Open in Telegram


C C Sharp programming Telegram | DID YOU KNOW?

Date: |

Tata Power whose core business is to generate, transmit and distribute electricity has made no money to investors in the last one decade. That is a big blunder considering it is one of the largest power generation companies in the country. One of the reasons is the company's huge debt levels which stood at ₹43,559 crore at the end of March 2021 compared to the company’s market capitalisation of ₹44,447 crore.

That strategy is the acquisition of a value-priced company by a growth company. Using the growth company's higher-priced stock for the acquisition can produce outsized revenue and earnings growth. Even better is the use of cash, particularly in a growth period when financial aggressiveness is accepted and even positively viewed.he key public rationale behind this strategy is synergy - the 1+1=3 view. In many cases, synergy does occur and is valuable. However, in other cases, particularly as the strategy gains popularity, it doesn't. Joining two different organizations, workforces and cultures is a challenge. Simply putting two separate organizations together necessarily creates disruptions and conflicts that can undermine both operations.

C C Sharp programming from id


Telegram C# (C Sharp) programming
FROM USA